package com.lcq.gulimall.member.service.impl;

import com.lcq.common.exception.PhoneExistException;
import com.lcq.common.exception.UserNameExistException;
import com.lcq.common.exception.UserNameORPasswordInvalidException;
import com.lcq.common.vo.MemberRsepVo;
import com.lcq.common.vo.UserShowInfoVo;
import com.lcq.gulimall.member.entity.MemberLevelEntity;
import com.lcq.gulimall.member.service.MemberLevelService;
import com.lcq.gulimall.member.vo.LoginVo;
import com.lcq.gulimall.member.vo.RegisterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcq.common.utils.PageUtils;
import com.lcq.common.utils.Query;

import com.lcq.gulimall.member.dao.MemberDao;
import com.lcq.gulimall.member.entity.MemberEntity;
import com.lcq.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    MemberLevelService memberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void register(RegisterVo registerVo) throws UserNameExistException, PhoneExistException {
        MemberEntity member = new MemberEntity();
        MemberLevelEntity levelEntity = memberLevelService.getDefaultLevel();
        if (levelEntity!=null){
            member.setLevelId(levelEntity.getId());
        }

        mobileUnique(registerVo.getPhone());
        member.setMobile(registerVo.getPhone());
        userNameUnique(registerVo.getUserName());
        member.setUsername(registerVo.getUserName());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        member.setPassword(passwordEncoder.encode(registerVo.getPassword()));
        this.save(member);
    }

    @Override
    public MemberRsepVo login(LoginVo loginVo) {
        MemberEntity memberEntity = this.getOne(new QueryWrapper<MemberEntity>()
                .eq("username", loginVo.getLoginAcc())
                .or().eq("mobile", loginVo.getLoginAcc()));
        if (memberEntity==null){
            throw new UserNameORPasswordInvalidException();
        }

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        boolean match = passwordEncoder.matches(loginVo.getPassword(), memberEntity.getPassword());
        if (!match){
            throw new UserNameORPasswordInvalidException();
        }
        /**
         * 登录成功保存用户信息到session
         * */
        MemberRsepVo user = new MemberRsepVo();
        BeanUtils.copyProperties(memberEntity,user);
        return user;
    }

    private void userNameUnique(String userName) throws UserNameExistException {
        int count = this.count(new QueryWrapper<MemberEntity>().eq("username", userName));
        if (count>0){
            throw new UserNameExistException();
        }
    }

    private void mobileUnique(String phone) throws PhoneExistException {
        int count = this.count(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (count>0){
            throw new PhoneExistException();
        }
    }

}